//
//  NSDictionary+Extensions.m
//  HYP
//
//  Created by yaofangyys on 14-7-30.
//  Copyright (c) 2014年 Guangdong No.1 Pharmacy Chain Co., Ltd. All rights reserved.
//

#import "NSDictionary+Extensions.h"

@implementation NSDictionary (Extensions)

- (id)mutableDeepCopy
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
    NSArray *keys = [self allKeys];
    
    for (id key in keys) {
        id value = [self objectForKey:key];
        
        if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
            id copyValue = nil;
            if ([value respondsToSelector:@selector(mutableDeepCopy)]) {
                copyValue = [value mutableDeepCopy];
                
            } else if([value respondsToSelector:@selector(mutableCopy)]) {
                copyValue = [value mutableCopy];
            }
            
            if (nil == copyValue) {
                copyValue = [value copy];
            }
            
            [dict setObject:copyValue forKey:key];
            copyValue = nil;
        } else {
            [dict setObject:value forKey:key];
        }
    }
    return dict;
}

- (NSString *)stringForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSString class]])
        return obj;
    return nil;
}

- (NSArray *)arrayForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSArray class]])
        return obj;
    return nil;
}

- (NSMutableArray *)mutableArrayForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSMutableArray class]])
        return obj;
    return nil;
}


- (NSDictionary *)dictionaryForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSDictionary class]])
    {
        return obj;
    }
    return nil;
}

- (NSMutableDictionary *)mutableDictionaryForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSMutableDictionary class]])
    {
        return obj;
    }
    return nil;
}

- (NSData *)dataForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSData class]])
        return obj;
    return nil;
}

- (NSInteger)integerForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && ([obj isKindOfClass:[NSString class]]
                       || [obj isKindOfClass:[NSNumber class]]))
    {
        return [obj integerValue];
    }
    return 0;
}

- (int)intForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && ([obj isKindOfClass:[NSString class]]
                       || [obj isKindOfClass:[NSNumber class]]))
    {
        return [obj intValue];
    }
    return 0;
}

- (float)floatForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && ([obj isKindOfClass:[NSString class]]
                       || [obj isKindOfClass:[NSNumber class]])) {
        return [obj floatValue];
    }
    return 0.0;
}

- (BOOL)boolForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && ([obj isKindOfClass:[NSString class]]
                       || [obj isKindOfClass:[NSNumber class]])) {
        return [obj boolValue];
    }
    return NO;
}

- (double)doubleForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && ([obj isKindOfClass:[NSString class]]
                       || [obj isKindOfClass:[NSNumber class]])) {
        return [obj doubleValue];
    }
    return 0.0;
}

- (NSNumber *)numberForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSNumber class]]) {
        return obj;
    }
    return nil;
}

- (NSArray *)stringArrayForKey:(NSString *)aKey
{
    id	arr = [self arrayForKey:aKey];
    
    if (arr != nil) {
        NSEnumerator *enumerator = [arr objectEnumerator];
        id obj;
        
        while ((obj = [enumerator nextObject])) {
            if ([obj isKindOfClass: [NSString class]] == NO) {
                return nil;
            }
        }
        return arr;
    }
    return nil;
}

- (NSDate *)dateForKey:(NSString *)aKey
{
    id	obj = [self objectForKey:aKey];
    
    if (obj != nil && [obj isKindOfClass:[NSDate class]]) {
        return obj;
    }
    return nil;
}

- (id)objForKey:(NSString *)aKey
{
    return [self objectForKey:aKey];
}

- (BOOL)hasValueForKey:(NSString *)aKey
{
    id obj = [self objectForKey:aKey];
    
    if (nil != obj && ![obj isKindOfClass:[[NSNull null] class]]) {
        return YES;
    }
    
    return NO;
}

@end

@implementation NSMutableDictionary (NSMutableDictionary_Extensions)

- (void)setInteger:(NSInteger)value forKey:(NSString *)aKey
{
    NSNumber *num = [[NSNumber class] numberWithInteger:value];
    
    [self setObject:num forKey:aKey];
}

- (void)setFloat:(float)value forKey:(NSString *)aKey
{
    NSNumber *num = [[NSNumber class] numberWithFloat:value];
    
    [self setObject:num forKey:aKey];
}

- (void)setDouble:(double)value forKey:(NSString *)aKey
{
    NSNumber *num = [[NSNumber class] numberWithDouble:value];
    
    [self setObject:num forKey:aKey];
}

- (void)setBool:(BOOL)value forKey:(NSString *)aKey
{
    NSNumber *num = [[NSNumber class] numberWithBool:value];
    
    [self setObject:num forKey:aKey];
}

- (void)setNumber:(NSNumber *)value forKey:(NSString *)aKey
{
    if ([value isKindOfClass:[NSNumber class]]) {
        [self setObject:value forKey:aKey];
    } else {
//        NSLog(@"%@ is not kind of class: %@ in %@",
//              NSStringFromClass([value class]),
//              NSStringFromClass([NSNumber class]),
//              NSStringFromSelector(_cmd));
    }
}

- (void)setString:(NSString *)value forKey:(NSString *)aKey
{
    if ([value isKindOfClass:[NSString class]]) {
        [self setObject:value forKey:aKey];
    } else {
//        NSLog(@"%@ is not kind of class: %@ in %@",
//              NSStringFromClass([value class]),
//              NSStringFromClass([NSString class]),
//              NSStringFromSelector(_cmd));
    }
}

- (void)setArray:(NSArray *)value forKey:(NSString *)aKey
{
    if ([value isKindOfClass:[NSArray class]]) {
        [self setObject:value forKey:aKey];
    } else {
//        HTLog(@"%@ is not kind of class: %@ in %@",
//              NSStringFromClass([value class]),
//              NSStringFromClass([NSArray class]),
//              NSStringFromSelector(_cmd));
    }
}

- (void)setDictionary:(NSDictionary *)value forKey:(NSString *)aKey
{
    if ([value isKindOfClass:[NSDictionary class]]) {
        [self setObject:value forKey:aKey];
    } else {
//        HTLog(@"%@ is not kind of class: %@ in %@",
//              NSStringFromClass([value class]),
//              NSStringFromClass([NSDictionary class]),
//              NSStringFromSelector(_cmd));
    }
}

- (void)setDate:(NSDate *)value forKey:(NSString *)aKey
{
    if ([value isKindOfClass:[NSDate class]]) {
        [self setObject:value forKey:aKey];
    } else {
//        HTLog(@"%@ is not kind of class: %@ in %@",
//              NSStringFromClass([value class]),
//              NSStringFromClass([NSDate class]),
//              NSStringFromSelector(_cmd));
    }
}

- (void)setData:(NSData *)value forKey:(NSString *)aKey
{
    if ([value isKindOfClass:[NSData class]]) {
        [self setObject:value forKey:aKey];
    } else {
//        HTLog(@"%@ is not kind of class: %@ in %@",
//              NSStringFromClass([value class]),
//              NSStringFromClass([NSData class]),
//              NSStringFromSelector(_cmd));
    }
}

- (void)setObj:(id)value forKey:(NSString *)aKey
{
    if (nil != value) {
        [self setObject:value forKey:aKey];
    } else {
//        HTLog(@"%@ is null in %@",
//              NSStringFromClass([value class]),
//              NSStringFromSelector(_cmd));
    }
}

@end
